package com.fashiontextile.user.service.impl;

import com.fashiontextile.admin.entity.Dict;
import com.fashiontextile.admin.feign.AdminClient;
import com.fashiontextile.common.entity.R;
import com.fashiontextile.common.utils.IdUtils;
import com.fashiontextile.user.entity.Article;
import com.fashiontextile.user.entity.Comment;
import com.fashiontextile.user.entity.Image;
import com.fashiontextile.user.feign.UserClient;
import com.fashiontextile.user.mapper.ArticleMapper;
import com.fashiontextile.user.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class ArticleServiceImpl implements ArticleService {

    private AdminClient adminClient;
    private IdUtils idUtils;
    private UserClient userClient;
    private ArticleMapper articleMapper;

    @Override
    public R<?> publishArticle(Long daid, Article article, Long uid) {
        Dict articleType = adminClient.selectDict(daid).getData();
        if (articleType == null) {
            return null;
        }
        long aid = idUtils.nextId();
        article.setAid(aid);
        article.setDictCategory(articleType);
        article.setUid(uid);
        List<Image> images = article.getImgs();
        for (Image image : images) {
            image.setPointTo(aid);
        }
        if (images.size() > 0) {
            if (!userClient.uploadImage(images).isSuccess()) {
                throw new RuntimeException("上传文章图片链接失败");
            }
        }
        Integer insertResult = articleMapper.insertArticle(article);
        return insertResult > 0 ? R.success(aid) : R.fail("上传失败");
    }

    @Override
    public R<?> upDateArticleByAid(Long uid, Article article) {
        article.setModified(new Date());
        Boolean isExist = articleMapper.selectArticleStatus(article.getAid());
        if (isExist == null) {
            return R.fail("文章不存在");
        }
        if (!isExist) {
            return R.fail("文章已被删除，无法修改");
        }
        if (!verifyModifyAuthority(uid, article.getAid())) {
            return R.fail("您没有权限编辑该文章");
        }
        Integer updateResult = articleMapper.upDateArticleByAid(article);
        return updateResult > 0 ? R.success("修改成功") : R.fail("修改失败");
    }

    @Override
    public boolean verifyModifyAuthority(Long uid, Long aid) {
        Long dataBaseUid = articleMapper.selectUidByAid(aid);
        return Objects.equals(uid, dataBaseUid);
    }

    @Override
    public R<?> deleteArticle(Long uid, Long aid) {
        Boolean isExist = articleMapper.selectArticleStatus(aid);
        if (isExist == null) {
            return R.fail("文章不存在");
        }
        if (!verifyModifyAuthority(uid, aid)) {
            return R.fail("您没有权限删除该文章");
        }
        Integer deleteResult = articleMapper.deleteArticle(aid);
        return deleteResult > 0 ? R.success("删除成功") : R.fail("删除失败");
    }

    @Override
    public R<?> selectArticles(Long dcid, Integer page) {
        PageHelper.startPage(page, 10);
        if (dcid == null) {
            List<Article> articles = articleMapper.selectNewArticle();
            PageInfo<Article> pageInfo = new PageInfo<>(articles);
            return R.success(pageInfo);
        } else {
            List<Article> articleList = articleMapper.selectArticleByType(dcid);
            PageInfo<Article> pageInfo = new PageInfo<>(articleList);
            return R.success(pageInfo);
        }
    }

    @Override
    public R<?> getArticleByUid(Long uid) {
        return R.success(articleMapper.getArticleByUid(uid));
    }

    @Override
    public R<?> getArticleByAid(Long aid) {
        Boolean isExist = articleMapper.selectArticleStatus(aid);
        if (isExist == null) {
            return R.fail("文章不存在");
        }
        if (!isExist) {
            return R.fail("文章已被删除，无法修改");
        }
        return R.success(articleMapper.getArticleByAid(aid));
    }

    @Override
    public R<?> insertComment(Long aid, Comment comment, Long uid) {
        comment.setCid(idUtils.nextId());
        comment.setCtid(aid);
        comment.setUid(uid);
        Integer insertResult = articleMapper.insertComment(comment);
        return insertResult > 0 ? R.success("评论成功") : R.fail("系统出错，评论失败！");
    }

    @Override
    public R<?> selectCommentByctid(Long aid) {
        List<Comment> parentComments = articleMapper.selectAllParantComment(aid);
        List<Long> parentCid=new ArrayList<>();
        for (Comment temp:parentComments) {
            parentCid.add(temp.getCid());
        }
        List<Comment> allChildreComment=articleMapper.selectAllChildrenComment(aid);
        List<Comment> result=handParentChildreComment(parentComments,allChildreComment);
        return R.success(result);
    }

    @Override
    public List<Comment> handParentChildreComment(List<Comment> parentComments,List<Comment> allChildreComment) {
        for(Comment parentCommen:parentComments){
            List<Comment> temp=new ArrayList<>();
            for (Comment childreComment:allChildreComment){
                if(childreComment.getParent().equals(parentCommen.getCid())){
                    temp.add(childreComment);
                }
            }
            parentCommen.setChildren(temp);
            handParentChildreComment(temp,allChildreComment);
        }
        return parentComments;
    }


}
